package io.renren.modules.carinfo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.JacksonUtils;
import io.renren.common.utils.PageData;
import io.renren.modules.carinfo.dao.CarInfoDao;
import io.renren.modules.carinfo.dto.CarInfoDTO;
import io.renren.modules.carinfo.entity.CarInfoEntity;
import io.renren.modules.carinfo.service.CarInfoService;
import io.renren.modules.carinfo.vo.CarInfoVO;
import io.renren.modules.carpicture.service.CarPictureService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 车辆信息表
 */
@Service("carInfoService")
@Slf4j
public class CarInfoServiceImpl extends ServiceImpl<CarInfoDao, CarInfoEntity> implements CarInfoService {
    @Autowired
    private CarPictureService carPictureService;
    @Override
    public PageData<CarInfoVO> queryCarInfoPage(Map<String, Object> params) {
        log.info("车辆信息表(分页)");
        // 获取信息
        String carNumber = (String) params.get("carNumber");
        String carModel = (String) params.get("carModel");
        Integer page = Integer.valueOf((String) params.get("page"));
        Integer limit = Integer.valueOf((String) params.get("limit"));
        Object status = params.get("status");
        QueryWrapper<CarInfoEntity> wrapper = new QueryWrapper<CarInfoEntity>();
        if (StringUtils.isNotBlank(carNumber)) {
            wrapper.eq("car_number",carNumber);
        }
        if (StringUtils.isNotBlank(carModel)) {
            wrapper.like("car_model",carModel);
        }
        if (status != null){
            wrapper.eq("status",status);
        }
        // 创建分页对象
        IPage<CarInfoEntity> iPage = new Page<CarInfoEntity>(page,limit);
        this.baseMapper.selectPage(iPage,wrapper);
        // 分页集合
        List<CarInfoEntity> records = iPage.getRecords();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<CarInfoVO> carInfoVOList = new ArrayList<>();
        for (CarInfoEntity record : records) {
            // 上市时间转换
            CarInfoVO carInfoVO = JacksonUtils.obj2pojo(record, CarInfoVO.class);
            // 创建时间
            carInfoVO.setUpdateTime(simpleDateFormat.format(record.getUpdateTime()));
            // 更新时间
            carInfoVO.setCreateTime(simpleDateFormat.format(record.getCreateTime()));
            // 车辆图片
            List<String> picture = carPictureService.getByCarInfoId(record.getId());
            if (CollectionUtil.isNotEmpty(picture)) {
                carInfoVO.setPicture(picture);
            }
            carInfoVOList.add(carInfoVO);
        }
        // 总数
        long total = iPage.getTotal();
        PageData<CarInfoVO> pageData = new PageData<CarInfoVO>(carInfoVOList,total);
        return pageData;
    }

    @Override
    public CarInfoVO queryById(Long id) {
        log.info("根据id查看车辆信息");
        CarInfoEntity entity = this.getById(id);
        if (entity != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            CarInfoVO carInfoVO = JacksonUtils.obj2pojo(entity, CarInfoVO.class);
            // 创建时间
            carInfoVO.setUpdateTime(simpleDateFormat.format(entity.getUpdateTime()));
            // 更新时间
            carInfoVO.setCreateTime(simpleDateFormat.format(entity.getCreateTime()));
            // 车辆图片
            List<String> picture = carPictureService.getByCarInfoId(entity.getId());
            if (CollectionUtil.isNotEmpty(picture)) {
                carInfoVO.setPicture(picture);
            }
            return carInfoVO;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCarInfo(CarInfoDTO dto) {
        log.info("新增车辆信息");
        // 生成车辆编号
        long time = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date(time));
        int number = (int)((Math.random()*9+1)*100000000);
        // 转换
        CarInfoEntity entity = JacksonUtils.obj2pojo(dto, CarInfoEntity.class);
        // 车辆编号
        entity.setCarNumber("BENZE" + "-" +format + "-" + number);
        this.baseMapper.insert(entity);
        // 获取车辆信息表id
        Long carInfoId = entity.getId();
        // 获取图片信息
        List<String> picture = dto.getPicture();
        // 保存
        carPictureService.savePictures(picture,carInfoId);
    }

    @Override
    public void carShelve(Long id, Integer status) {
        log.info("车辆上下架(0:下架 1:上架)");
        CarInfoEntity carInfoEntity = new CarInfoEntity();
        carInfoEntity.setStatus(status);
        UpdateWrapper<CarInfoEntity> updateWrapper = new UpdateWrapper<CarInfoEntity>();
        updateWrapper.eq("id",id);
        this.baseMapper.update(carInfoEntity,updateWrapper);
    }

    @Override
    public void deleteByIds(List<Long> asList) {
        log.info("删除车辆信息");
        this.baseMapper.deleteBatchIds(asList);
    }

    @Override
    public void editById(CarInfoDTO dto) {
        log.info("修改车辆信息");
        CarInfoEntity carInfoEntity = JacksonUtils.obj2pojo(dto, CarInfoEntity.class);
        this.baseMapper.updateById(carInfoEntity);
        // 获取车辆信息表id
        Long carInfoId = carInfoEntity.getId();
        // 获取图片信息
        List<String> picture = dto.getPicture();
        // 删除原有数据
        carPictureService.deleteBy(carInfoId);
        // 保存
        carPictureService.savePictures(picture,carInfoId);
    }

    @Override
    public List<CarInfoEntity> getCarNum(Map<String, Object> params) {
        log.info("获取车辆编号");
        List<CarInfoEntity> infoEntities = this.baseMapper.selectList(null);
        return infoEntities;
    }

    @Override
    public CarInfoEntity queryByNum(String num) {
        QueryWrapper<CarInfoEntity> wrapper = new QueryWrapper<CarInfoEntity>();
        wrapper.eq("car_number",num);
        return this.baseMapper.selectOne(wrapper);
    }

}
